{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# AdaBoost封装函数：单项功能"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-01-13T16:22:16.782989Z",
     "start_time": "2020-01-13T16:22:16.779002Z"
    }
   },
   "outputs": [],
   "source": [
    "import math"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 初始变量"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 输入数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-01-13T16:22:17.283546Z",
     "start_time": "2020-01-13T16:22:17.279556Z"
    }
   },
   "outputs": [],
   "source": [
    "x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
    "y = [1, 1, 1, -1, -1, -1, 1, 1, 1, -1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 个体训练器数量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-01-13T16:22:17.673550Z",
     "start_time": "2020-01-13T16:22:17.670995Z"
    }
   },
   "outputs": [],
   "source": [
    "T = 3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 初始权重"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-01-13T16:22:19.619897Z",
     "start_time": "2020-01-13T16:22:19.608926Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def func_w_1(x):\n",
    "    w_1 = []\n",
    "    for i in range(len(x)):\n",
    "        w_1.append(0.1)\n",
    "    return w_1\n",
    "w_1 = func_w_1(x)\n",
    "w_1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-01-13T16:22:20.257000Z",
     "start_time": "2020-01-13T16:22:20.253047Z"
    }
   },
   "outputs": [],
   "source": [
    "w_n = w_1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 设定阈值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-01-13T16:22:22.280994Z",
     "start_time": "2020-01-13T16:22:22.275010Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[-0.5, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 生成阈值函数\n",
    "def func_threshs(x):\n",
    "    threshs = [i - 0.5 for i in x]\n",
    "    threshs.append(x[len(x)-1]+0.5)\n",
    "    return threshs\n",
    "threshs = func_threshs(x)\n",
    "threshs"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 循环内函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-01-13T16:22:33.234617Z",
     "start_time": "2020-01-13T16:22:33.224606Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "({-0.5: [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1],\n",
       "  0.5: [1, -1, -1, -1, -1, -1, -1, -1, -1, -1],\n",
       "  1.5: [1, 1, -1, -1, -1, -1, -1, -1, -1, -1],\n",
       "  2.5: [1, 1, 1, -1, -1, -1, -1, -1, -1, -1],\n",
       "  3.5: [1, 1, 1, 1, -1, -1, -1, -1, -1, -1],\n",
       "  4.5: [1, 1, 1, 1, 1, -1, -1, -1, -1, -1],\n",
       "  5.5: [1, 1, 1, 1, 1, 1, -1, -1, -1, -1],\n",
       "  6.5: [1, 1, 1, 1, 1, 1, 1, -1, -1, -1],\n",
       "  7.5: [1, 1, 1, 1, 1, 1, 1, 1, -1, -1],\n",
       "  8.5: [1, 1, 1, 1, 1, 1, 1, 1, 1, -1],\n",
       "  9.5: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]},\n",
       " {-0.5: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n",
       "  0.5: [-1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n",
       "  1.5: [-1, -1, 1, 1, 1, 1, 1, 1, 1, 1],\n",
       "  2.5: [-1, -1, -1, 1, 1, 1, 1, 1, 1, 1],\n",
       "  3.5: [-1, -1, -1, -1, 1, 1, 1, 1, 1, 1],\n",
       "  4.5: [-1, -1, -1, -1, -1, 1, 1, 1, 1, 1],\n",
       "  5.5: [-1, -1, -1, -1, -1, -1, 1, 1, 1, 1],\n",
       "  6.5: [-1, -1, -1, -1, -1, -1, -1, 1, 1, 1],\n",
       "  7.5: [-1, -1, -1, -1, -1, -1, -1, -1, 1, 1],\n",
       "  8.5: [-1, -1, -1, -1, -1, -1, -1, -1, -1, 1],\n",
       "  9.5: [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1]})"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 所有阈值分割结果：临时判定小于阈值为1，大于阈值为-1\n",
    "def func_y_pres_all_thresh_tmp(threshs):\n",
    "    y_pres_all_thresh_tmp_ps = {} # 判定小于阈值为1，大于阈值为-1\n",
    "    y_pres_all_thresh_tmp_ng = {}  # 相反\n",
    "    for thresh in threshs:\n",
    "        y_pres_ps = []\n",
    "        y_pres_ng = []\n",
    "        for i in range(len(x)):\n",
    "            if x[i] < thresh:\n",
    "                y_pres_ps.append(1)\n",
    "                y_pres_ng.append(-1)\n",
    "            else:\n",
    "                y_pres_ps.append(-1)\n",
    "                y_pres_ng.append(1)\n",
    "        y_pres_all_thresh_tmp_ps[thresh] = y_pres_ps\n",
    "        y_pres_all_thresh_tmp_ng[thresh] = y_pres_ng\n",
    "    return y_pres_all_thresh_tmp_ps, y_pres_all_thresh_tmp_ng\n",
    "y_pres_all_thresh_tmp_ps, y_pres_all_thresh_tmp_ng = func_y_pres_all_thresh_tmp(threshs)\n",
    "y_pres_all_thresh_tmp_ps, y_pres_all_thresh_tmp_ng"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-01-13T16:25:09.492892Z",
     "start_time": "2020-01-13T16:25:09.482919Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.4"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 计算每个阈值的错误率\n",
    "def sub_func_e(y,w_n,y_pres_all_thresh_tmp,thresh):\n",
    "    e = 0\n",
    "    for i in range(len(y)):\n",
    "        if y_pres_all_thresh_tmp[thresh][i]!= y[i]:\n",
    "            e+= w_n[i]\n",
    "    return e\n",
    "thresh = 1.5\n",
    "e = sub_func_e(y,w_n,y_pres_all_thresh_tmp_ps,thresh)\n",
    "e"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-01-13T16:26:25.251845Z",
     "start_time": "2020-01-13T16:26:25.243865Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "({-0.5: 0.6,\n",
       "  0.5: 0.5,\n",
       "  1.5: 0.4,\n",
       "  2.5: 0.3,\n",
       "  3.5: 0.4,\n",
       "  4.5: 0.5,\n",
       "  5.5: 0.6,\n",
       "  6.5: 0.5,\n",
       "  7.5: 0.4,\n",
       "  8.5: 0.3,\n",
       "  9.5: 0.4},\n",
       " 0.3,\n",
       " 2.5)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算所有阈值的错误率\n",
    "def sub_func_e_s(y,threshs,w_n,y_pres_all_thresh_tmp):\n",
    "    e_s = {}\n",
    "    e_min = 1 # 错误率最小值初始值设为1\n",
    "    n = 0\n",
    "    for thresh in threshs:\n",
    "        e = sub_func_e(y,w_n,y_pres_all_thresh_tmp,thresh)\n",
    "        e_s[thresh] = round(e,6)\n",
    "        if e < e_min:\n",
    "            e_min = round(e, 6)\n",
    "            n = thresh\n",
    "    return e_s, e_min, n\n",
    "\n",
    "e_s, e_min, n = sub_func_e_s(y,threshs, w_n,y_pres_all_thresh_tmp_ps)\n",
    "e_s, e_min, n "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-01-13T16:29:07.239837Z",
     "start_time": "2020-01-13T16:29:07.230769Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "({-0.5: 0.6,\n",
       "  0.5: 0.5,\n",
       "  1.5: 0.4,\n",
       "  2.5: 0.3,\n",
       "  3.5: 0.4,\n",
       "  4.5: 0.5,\n",
       "  5.5: 0.6,\n",
       "  6.5: 0.5,\n",
       "  7.5: 0.4,\n",
       "  8.5: 0.3,\n",
       "  9.5: 0.4},\n",
       " 0.3,\n",
       " 2.5,\n",
       " {-0.5: [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1],\n",
       "  0.5: [1, -1, -1, -1, -1, -1, -1, -1, -1, -1],\n",
       "  1.5: [1, 1, -1, -1, -1, -1, -1, -1, -1, -1],\n",
       "  2.5: [1, 1, 1, -1, -1, -1, -1, -1, -1, -1],\n",
       "  3.5: [1, 1, 1, 1, -1, -1, -1, -1, -1, -1],\n",
       "  4.5: [1, 1, 1, 1, 1, -1, -1, -1, -1, -1],\n",
       "  5.5: [1, 1, 1, 1, 1, 1, -1, -1, -1, -1],\n",
       "  6.5: [1, 1, 1, 1, 1, 1, 1, -1, -1, -1],\n",
       "  7.5: [1, 1, 1, 1, 1, 1, 1, 1, -1, -1],\n",
       "  8.5: [1, 1, 1, 1, 1, 1, 1, 1, 1, -1],\n",
       "  9.5: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]})"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算所有阈值对应加权错误率函数，并找到最小错误率对应的判断结果\n",
    "def func_e_s(x,y,threshs,w_n,y_pres_all_thresh_tmp_ps, y_pres_all_thresh_tmp_ng):\n",
    "    e_s_ps, e_min_ps, n_ps = sub_func_e_s(y,threshs,w_n,y_pres_all_thresh_tmp_ps)\n",
    "    e_s_ng, e_min_ng, n_ng = sub_func_e_s(y,threshs,w_n,y_pres_all_thresh_tmp_ng)\n",
    "    if e_min_ps <= e_min_ng:\n",
    "        return e_s_ps, e_min_ps, n_ps, y_pres_all_thresh_tmp_ps\n",
    "    else:\n",
    "        return e_s_ng, e_min_ng, n_ng, y_pres_all_thresh_tmp_ng\n",
    "    \n",
    "\n",
    "e_s, e_min, n, y_pres_all_thresh = func_e_s(x, y, threshs, w_n, y_pres_all_thresh_tmp_ps, y_pres_all_thresh_tmp_ng)\n",
    "e_s, e_min, n, y_pres_all_thresh"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-01-13T16:29:54.217577Z",
     "start_time": "2020-01-13T16:29:54.212589Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 1, 1, -1, -1, -1, -1, -1, -1, -1]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获得最佳阈值对应预测的函数\n",
    "def func_pre_n(y_pres_all_thresh, n):\n",
    "    pre_n = y_pres_all_thresh[n]\n",
    "    return pre_n\n",
    "pre_n = func_pre_n(y_pres_all_thresh, n)\n",
    "pre_n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-01-13T16:30:47.905001Z",
     "start_time": "2020-01-13T16:30:47.895827Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.423649"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算模型权重a\n",
    "def func_a_n(e_min):\n",
    "    a_n = 0.5 * math.log((1 - e_min)/e_min)\n",
    "    a_n = round(a_n, 6) # 保留6位小数\n",
    "    return a_n\n",
    "a_n = func_a_n(e_min)\n",
    "a_n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-01-13T16:31:34.740561Z",
     "start_time": "2020-01-13T16:31:34.731584Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0.065465,\n",
       " 0.065465,\n",
       " 0.065465,\n",
       " 0.065465,\n",
       " 0.065465,\n",
       " 0.065465,\n",
       " 0.152753,\n",
       " 0.152753,\n",
       " 0.152753,\n",
       " 0.065465]"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算权重暂时值\n",
    "def func_w_n_tmp(x,pre_n,w_n,a_n):\n",
    "    w_n_tmp = []\n",
    "    for i in range(len(x)):\n",
    "        w_new = w_n[i]*math.exp(-1*a_n*y[i]*pre_n[i])\n",
    "        w_new = round(w_new, 6)\n",
    "        w_n_tmp.append(w_new)\n",
    "    return w_n_tmp\n",
    "w_n_tmp = func_w_n_tmp(x, pre_n,w_1,a_n)\n",
    "w_n_tmp "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-01-13T16:32:34.557923Z",
     "start_time": "2020-01-13T16:32:34.548923Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.916514"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算z\n",
    "def func_z_n(w_n_tmp):\n",
    "    z_n = sum(w_n_tmp)\n",
    "    return z_n\n",
    "z_n = func_z_n(w_n_tmp)\n",
    "z_n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-01-13T16:32:54.582520Z",
     "start_time": "2020-01-13T16:32:54.576536Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0.07143,\n",
       " 0.07143,\n",
       " 0.07143,\n",
       " 0.07143,\n",
       " 0.07143,\n",
       " 0.07143,\n",
       " 0.16667,\n",
       " 0.16667,\n",
       " 0.16667,\n",
       " 0.07143]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算调整后权重\n",
    "def func_w_n(w_n_tmp, z_n):\n",
    "    w_n = [round(i/z_n, 5) for i in w_n_tmp]\n",
    "    return w_n\n",
    "w_n = func_w_n(w_n_tmp, z_n)\n",
    "w_n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-01-13T16:33:10.200732Z",
     "start_time": "2020-01-13T16:33:10.193932Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0.423649,\n",
       " 0.423649,\n",
       " 0.423649,\n",
       " -0.423649,\n",
       " -0.423649,\n",
       " -0.423649,\n",
       " -0.423649,\n",
       " -0.423649,\n",
       " -0.423649,\n",
       " -0.423649]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算第一个模型加权后的预测\n",
    "def func_g_n(x, pre_n, a_n):\n",
    "    g_n = []\n",
    "    for i in range(len(x)):\n",
    "        g_n_single = a_n * pre_n[i]\n",
    "        g_n.append(g_n_single)\n",
    "    return g_n\n",
    "g_n = func_g_n(x, pre_n, a_n)\n",
    "g_n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## AdaBoost算法实现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-01-13T16:41:09.137217Z",
     "start_time": "2020-01-13T16:41:09.129241Z"
    }
   },
   "outputs": [],
   "source": [
    "def func_AdaBoost(x, y, T):\n",
    "    \n",
    "    # 储存变量的列表\n",
    "    a_n_s = [] # 存放个体学习器权重\n",
    "    w_n_s = [] # 存放样本权重\n",
    "    pre_n_s = [] # 存放预测值\n",
    "    g_n_s = []  # 存放加权后预测值\n",
    "    n_s = []\n",
    "\n",
    "    \n",
    "    # 权重初始化\n",
    "    w_1 = func_w_1(x)    \n",
    "    w_n_s.append(w_1)\n",
    "    \n",
    "    # 生成阈值\n",
    "    threshs = func_threshs(x)\n",
    "    \n",
    "    for i in range(3):  \n",
    "    \n",
    "        # 变量权重\n",
    "        w_n = w_n_s[i]\n",
    "        # 所有阈值二分法结果 positive 小于阈值为1，大于阈值为-1，negative相反\n",
    "        y_pres_all_thresh_tmp_ps, y_pres_all_thresh_tmp_ng = func_y_pres_all_thresh_tmp(threshs)\n",
    "        # 计算一个阈值尝试两种判定，计算所有情况加权后错误率，最小错误率，及对应的阈值\n",
    "        e_s, e_min, n, y_pres_all_thresh = func_e_s(x, y, threshs, w_n, y_pres_all_thresh_tmp_ps, y_pres_all_thresh_tmp_ng)\n",
    "        # 获得最佳阈值对应预测\n",
    "        pre_n = func_pre_n(y_pres_all_thresh, n)\n",
    "        # 计算模型权重a\n",
    "        a_n = func_a_n(e_min)\n",
    "        # 计算权重暂时值\n",
    "        w_n_tmp = func_w_n_tmp(x, pre_n,w_n,a_n)\n",
    "        # 计算z\n",
    "        z_n = func_z_n(w_n_tmp)\n",
    "        # 计算调整后权重\n",
    "        w_n = func_w_n(w_n_tmp, z_n)\n",
    "        # 计算第一个模型加权后的预测\n",
    "        g_n = func_g_n(x, pre_n, a_n)\n",
    "        \n",
    "        # 数据存储\n",
    "        a_n_s.append(a_n) # 存放个体学习器权重\n",
    "        w_n_s.append(w_n)# 存放样本权重\n",
    "        pre_n_s.append(pre_n)# 存放预测值\n",
    "        g_n_s.append(g_n)\n",
    "        n_s.append(n)\n",
    "\n",
    "        \n",
    "    return a_n_s, w_n_s, pre_n_s, g_n_s, n_s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-01-13T16:41:11.941884Z",
     "start_time": "2020-01-13T16:41:11.936862Z"
    }
   },
   "outputs": [],
   "source": [
    "a_n_s, w_n_s, pre_n_s, g_n_s, n_s= func_AdaBoost(x, y, T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-01-13T16:41:14.219770Z",
     "start_time": "2020-01-13T16:41:14.214778Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0.423649, 0.649629, 0.751965]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a_n_s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-01-13T16:41:55.882561Z",
     "start_time": "2020-01-13T16:41:55.876576Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2.5, 8.5, 5.5]"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n_s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-01-13T16:42:01.460073Z",
     "start_time": "2020-01-13T16:42:01.455087Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1],\n",
       " [0.07143,\n",
       "  0.07143,\n",
       "  0.07143,\n",
       "  0.07143,\n",
       "  0.07143,\n",
       "  0.07143,\n",
       "  0.16667,\n",
       "  0.16667,\n",
       "  0.16667,\n",
       "  0.07143],\n",
       " [0.04546,\n",
       "  0.04546,\n",
       "  0.04546,\n",
       "  0.16666,\n",
       "  0.16666,\n",
       "  0.16666,\n",
       "  0.10606,\n",
       "  0.10606,\n",
       "  0.10606,\n",
       "  0.04546],\n",
       " [0.125,\n",
       "  0.125,\n",
       "  0.125,\n",
       "  0.10185,\n",
       "  0.10185,\n",
       "  0.10185,\n",
       "  0.06482,\n",
       "  0.06482,\n",
       "  0.06482,\n",
       "  0.125]]"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "w_n_s"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 查看结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-01-13T16:42:10.043021Z",
     "start_time": "2020-01-13T16:42:09.566312Z"
    },
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "data = {'x':x, \"y\":y, \n",
    "        \"w_1\":w_n_s[0],'pre_1':pre_n_s[0], 'g_1':g_n_s[0],\n",
    "        \"w_2\":w_n_s[1],'pre_2':pre_n_s[1], 'g_2':g_n_s[1],\n",
    "        \"w_3\":w_n_s[2],'pre_3':pre_n_s[2], 'g_3':g_n_s[2],\n",
    "        \"w_4\":w_n_s[3]\n",
    "        \n",
    "       }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-01-13T16:42:11.569245Z",
     "start_time": "2020-01-13T16:42:11.541318Z"
    },
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "      <th>5</th>\n",
       "      <th>6</th>\n",
       "      <th>7</th>\n",
       "      <th>8</th>\n",
       "      <th>9</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>x</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>4.000000</td>\n",
       "      <td>5.000000</td>\n",
       "      <td>6.000000</td>\n",
       "      <td>7.000000</td>\n",
       "      <td>8.000000</td>\n",
       "      <td>9.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>y</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>w_1</th>\n",
       "      <td>0.100000</td>\n",
       "      <td>0.100000</td>\n",
       "      <td>0.100000</td>\n",
       "      <td>0.100000</td>\n",
       "      <td>0.100000</td>\n",
       "      <td>0.100000</td>\n",
       "      <td>0.100000</td>\n",
       "      <td>0.100000</td>\n",
       "      <td>0.100000</td>\n",
       "      <td>0.100000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>pre_1</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>g_1</th>\n",
       "      <td>0.423649</td>\n",
       "      <td>0.423649</td>\n",
       "      <td>0.423649</td>\n",
       "      <td>-0.423649</td>\n",
       "      <td>-0.423649</td>\n",
       "      <td>-0.423649</td>\n",
       "      <td>-0.423649</td>\n",
       "      <td>-0.423649</td>\n",
       "      <td>-0.423649</td>\n",
       "      <td>-0.423649</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>w_2</th>\n",
       "      <td>0.071430</td>\n",
       "      <td>0.071430</td>\n",
       "      <td>0.071430</td>\n",
       "      <td>0.071430</td>\n",
       "      <td>0.071430</td>\n",
       "      <td>0.071430</td>\n",
       "      <td>0.166670</td>\n",
       "      <td>0.166670</td>\n",
       "      <td>0.166670</td>\n",
       "      <td>0.071430</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>pre_2</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>g_2</th>\n",
       "      <td>0.649629</td>\n",
       "      <td>0.649629</td>\n",
       "      <td>0.649629</td>\n",
       "      <td>0.649629</td>\n",
       "      <td>0.649629</td>\n",
       "      <td>0.649629</td>\n",
       "      <td>0.649629</td>\n",
       "      <td>0.649629</td>\n",
       "      <td>0.649629</td>\n",
       "      <td>-0.649629</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>w_3</th>\n",
       "      <td>0.045460</td>\n",
       "      <td>0.045460</td>\n",
       "      <td>0.045460</td>\n",
       "      <td>0.166660</td>\n",
       "      <td>0.166660</td>\n",
       "      <td>0.166660</td>\n",
       "      <td>0.106060</td>\n",
       "      <td>0.106060</td>\n",
       "      <td>0.106060</td>\n",
       "      <td>0.045460</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>pre_3</th>\n",
       "      <td>-1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>g_3</th>\n",
       "      <td>-0.751965</td>\n",
       "      <td>-0.751965</td>\n",
       "      <td>-0.751965</td>\n",
       "      <td>-0.751965</td>\n",
       "      <td>-0.751965</td>\n",
       "      <td>-0.751965</td>\n",
       "      <td>0.751965</td>\n",
       "      <td>0.751965</td>\n",
       "      <td>0.751965</td>\n",
       "      <td>0.751965</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>w_4</th>\n",
       "      <td>0.125000</td>\n",
       "      <td>0.125000</td>\n",
       "      <td>0.125000</td>\n",
       "      <td>0.101850</td>\n",
       "      <td>0.101850</td>\n",
       "      <td>0.101850</td>\n",
       "      <td>0.064820</td>\n",
       "      <td>0.064820</td>\n",
       "      <td>0.064820</td>\n",
       "      <td>0.125000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              0         1         2         3         4         5         6  \\\n",
       "x      0.000000  1.000000  2.000000  3.000000  4.000000  5.000000  6.000000   \n",
       "y      1.000000  1.000000  1.000000 -1.000000 -1.000000 -1.000000  1.000000   \n",
       "w_1    0.100000  0.100000  0.100000  0.100000  0.100000  0.100000  0.100000   \n",
       "pre_1  1.000000  1.000000  1.000000 -1.000000 -1.000000 -1.000000 -1.000000   \n",
       "g_1    0.423649  0.423649  0.423649 -0.423649 -0.423649 -0.423649 -0.423649   \n",
       "w_2    0.071430  0.071430  0.071430  0.071430  0.071430  0.071430  0.166670   \n",
       "pre_2  1.000000  1.000000  1.000000  1.000000  1.000000  1.000000  1.000000   \n",
       "g_2    0.649629  0.649629  0.649629  0.649629  0.649629  0.649629  0.649629   \n",
       "w_3    0.045460  0.045460  0.045460  0.166660  0.166660  0.166660  0.106060   \n",
       "pre_3 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000  1.000000   \n",
       "g_3   -0.751965 -0.751965 -0.751965 -0.751965 -0.751965 -0.751965  0.751965   \n",
       "w_4    0.125000  0.125000  0.125000  0.101850  0.101850  0.101850  0.064820   \n",
       "\n",
       "              7         8         9  \n",
       "x      7.000000  8.000000  9.000000  \n",
       "y      1.000000  1.000000 -1.000000  \n",
       "w_1    0.100000  0.100000  0.100000  \n",
       "pre_1 -1.000000 -1.000000 -1.000000  \n",
       "g_1   -0.423649 -0.423649 -0.423649  \n",
       "w_2    0.166670  0.166670  0.071430  \n",
       "pre_2  1.000000  1.000000 -1.000000  \n",
       "g_2    0.649629  0.649629 -0.649629  \n",
       "w_3    0.106060  0.106060  0.045460  \n",
       "pre_3  1.000000  1.000000  1.000000  \n",
       "g_3    0.751965  0.751965  0.751965  \n",
       "w_4    0.064820  0.064820  0.125000  "
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame(data)\n",
    "df.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-01-13T16:42:53.224640Z",
     "start_time": "2020-01-13T16:42:53.206657Z"
    }
   },
   "outputs": [],
   "source": [
    "df['SUM']=df['g_1'] + df['g_2'] + df['g_3']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-01-13T16:43:18.235596Z",
     "start_time": "2020-01-13T16:43:18.217590Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "      <th>5</th>\n",
       "      <th>6</th>\n",
       "      <th>7</th>\n",
       "      <th>8</th>\n",
       "      <th>9</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>x</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>4.000000</td>\n",
       "      <td>5.000000</td>\n",
       "      <td>6.000000</td>\n",
       "      <td>7.000000</td>\n",
       "      <td>8.000000</td>\n",
       "      <td>9.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>y</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>w_1</th>\n",
       "      <td>0.100000</td>\n",
       "      <td>0.100000</td>\n",
       "      <td>0.100000</td>\n",
       "      <td>0.100000</td>\n",
       "      <td>0.100000</td>\n",
       "      <td>0.100000</td>\n",
       "      <td>0.100000</td>\n",
       "      <td>0.100000</td>\n",
       "      <td>0.100000</td>\n",
       "      <td>0.100000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>pre_1</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>g_1</th>\n",
       "      <td>0.423649</td>\n",
       "      <td>0.423649</td>\n",
       "      <td>0.423649</td>\n",
       "      <td>-0.423649</td>\n",
       "      <td>-0.423649</td>\n",
       "      <td>-0.423649</td>\n",
       "      <td>-0.423649</td>\n",
       "      <td>-0.423649</td>\n",
       "      <td>-0.423649</td>\n",
       "      <td>-0.423649</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>w_2</th>\n",
       "      <td>0.071430</td>\n",
       "      <td>0.071430</td>\n",
       "      <td>0.071430</td>\n",
       "      <td>0.071430</td>\n",
       "      <td>0.071430</td>\n",
       "      <td>0.071430</td>\n",
       "      <td>0.166670</td>\n",
       "      <td>0.166670</td>\n",
       "      <td>0.166670</td>\n",
       "      <td>0.071430</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>pre_2</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>g_2</th>\n",
       "      <td>0.649629</td>\n",
       "      <td>0.649629</td>\n",
       "      <td>0.649629</td>\n",
       "      <td>0.649629</td>\n",
       "      <td>0.649629</td>\n",
       "      <td>0.649629</td>\n",
       "      <td>0.649629</td>\n",
       "      <td>0.649629</td>\n",
       "      <td>0.649629</td>\n",
       "      <td>-0.649629</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>w_3</th>\n",
       "      <td>0.045460</td>\n",
       "      <td>0.045460</td>\n",
       "      <td>0.045460</td>\n",
       "      <td>0.166660</td>\n",
       "      <td>0.166660</td>\n",
       "      <td>0.166660</td>\n",
       "      <td>0.106060</td>\n",
       "      <td>0.106060</td>\n",
       "      <td>0.106060</td>\n",
       "      <td>0.045460</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>pre_3</th>\n",
       "      <td>-1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>g_3</th>\n",
       "      <td>-0.751965</td>\n",
       "      <td>-0.751965</td>\n",
       "      <td>-0.751965</td>\n",
       "      <td>-0.751965</td>\n",
       "      <td>-0.751965</td>\n",
       "      <td>-0.751965</td>\n",
       "      <td>0.751965</td>\n",
       "      <td>0.751965</td>\n",
       "      <td>0.751965</td>\n",
       "      <td>0.751965</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>w_4</th>\n",
       "      <td>0.125000</td>\n",
       "      <td>0.125000</td>\n",
       "      <td>0.125000</td>\n",
       "      <td>0.101850</td>\n",
       "      <td>0.101850</td>\n",
       "      <td>0.101850</td>\n",
       "      <td>0.064820</td>\n",
       "      <td>0.064820</td>\n",
       "      <td>0.064820</td>\n",
       "      <td>0.125000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SUM</th>\n",
       "      <td>0.321313</td>\n",
       "      <td>0.321313</td>\n",
       "      <td>0.321313</td>\n",
       "      <td>-0.525985</td>\n",
       "      <td>-0.525985</td>\n",
       "      <td>-0.525985</td>\n",
       "      <td>0.977945</td>\n",
       "      <td>0.977945</td>\n",
       "      <td>0.977945</td>\n",
       "      <td>-0.321313</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              0         1         2         3         4         5         6  \\\n",
       "x      0.000000  1.000000  2.000000  3.000000  4.000000  5.000000  6.000000   \n",
       "y      1.000000  1.000000  1.000000 -1.000000 -1.000000 -1.000000  1.000000   \n",
       "w_1    0.100000  0.100000  0.100000  0.100000  0.100000  0.100000  0.100000   \n",
       "pre_1  1.000000  1.000000  1.000000 -1.000000 -1.000000 -1.000000 -1.000000   \n",
       "g_1    0.423649  0.423649  0.423649 -0.423649 -0.423649 -0.423649 -0.423649   \n",
       "w_2    0.071430  0.071430  0.071430  0.071430  0.071430  0.071430  0.166670   \n",
       "pre_2  1.000000  1.000000  1.000000  1.000000  1.000000  1.000000  1.000000   \n",
       "g_2    0.649629  0.649629  0.649629  0.649629  0.649629  0.649629  0.649629   \n",
       "w_3    0.045460  0.045460  0.045460  0.166660  0.166660  0.166660  0.106060   \n",
       "pre_3 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000  1.000000   \n",
       "g_3   -0.751965 -0.751965 -0.751965 -0.751965 -0.751965 -0.751965  0.751965   \n",
       "w_4    0.125000  0.125000  0.125000  0.101850  0.101850  0.101850  0.064820   \n",
       "SUM    0.321313  0.321313  0.321313 -0.525985 -0.525985 -0.525985  0.977945   \n",
       "\n",
       "              7         8         9  \n",
       "x      7.000000  8.000000  9.000000  \n",
       "y      1.000000  1.000000 -1.000000  \n",
       "w_1    0.100000  0.100000  0.100000  \n",
       "pre_1 -1.000000 -1.000000 -1.000000  \n",
       "g_1   -0.423649 -0.423649 -0.423649  \n",
       "w_2    0.166670  0.166670  0.071430  \n",
       "pre_2  1.000000  1.000000 -1.000000  \n",
       "g_2    0.649629  0.649629 -0.649629  \n",
       "w_3    0.106060  0.106060  0.045460  \n",
       "pre_3  1.000000  1.000000  1.000000  \n",
       "g_3    0.751965  0.751965  0.751965  \n",
       "w_4    0.064820  0.064820  0.125000  \n",
       "SUM    0.977945  0.977945 -0.321313  "
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-01-13T16:43:32.489406Z",
     "start_time": "2020-01-13T16:43:32.481428Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "df['G(x)']=np.sign(df['SUM'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-01-13T16:43:38.939735Z",
     "start_time": "2020-01-13T16:43:38.925772Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "      <th>5</th>\n",
       "      <th>6</th>\n",
       "      <th>7</th>\n",
       "      <th>8</th>\n",
       "      <th>9</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>x</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>4.000000</td>\n",
       "      <td>5.000000</td>\n",
       "      <td>6.000000</td>\n",
       "      <td>7.000000</td>\n",
       "      <td>8.000000</td>\n",
       "      <td>9.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>y</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>w_1</th>\n",
       "      <td>0.100000</td>\n",
       "      <td>0.100000</td>\n",
       "      <td>0.100000</td>\n",
       "      <td>0.100000</td>\n",
       "      <td>0.100000</td>\n",
       "      <td>0.100000</td>\n",
       "      <td>0.100000</td>\n",
       "      <td>0.100000</td>\n",
       "      <td>0.100000</td>\n",
       "      <td>0.100000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>pre_1</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>g_1</th>\n",
       "      <td>0.423649</td>\n",
       "      <td>0.423649</td>\n",
       "      <td>0.423649</td>\n",
       "      <td>-0.423649</td>\n",
       "      <td>-0.423649</td>\n",
       "      <td>-0.423649</td>\n",
       "      <td>-0.423649</td>\n",
       "      <td>-0.423649</td>\n",
       "      <td>-0.423649</td>\n",
       "      <td>-0.423649</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>w_2</th>\n",
       "      <td>0.071430</td>\n",
       "      <td>0.071430</td>\n",
       "      <td>0.071430</td>\n",
       "      <td>0.071430</td>\n",
       "      <td>0.071430</td>\n",
       "      <td>0.071430</td>\n",
       "      <td>0.166670</td>\n",
       "      <td>0.166670</td>\n",
       "      <td>0.166670</td>\n",
       "      <td>0.071430</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>pre_2</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>g_2</th>\n",
       "      <td>0.649629</td>\n",
       "      <td>0.649629</td>\n",
       "      <td>0.649629</td>\n",
       "      <td>0.649629</td>\n",
       "      <td>0.649629</td>\n",
       "      <td>0.649629</td>\n",
       "      <td>0.649629</td>\n",
       "      <td>0.649629</td>\n",
       "      <td>0.649629</td>\n",
       "      <td>-0.649629</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>w_3</th>\n",
       "      <td>0.045460</td>\n",
       "      <td>0.045460</td>\n",
       "      <td>0.045460</td>\n",
       "      <td>0.166660</td>\n",
       "      <td>0.166660</td>\n",
       "      <td>0.166660</td>\n",
       "      <td>0.106060</td>\n",
       "      <td>0.106060</td>\n",
       "      <td>0.106060</td>\n",
       "      <td>0.045460</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>pre_3</th>\n",
       "      <td>-1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>g_3</th>\n",
       "      <td>-0.751965</td>\n",
       "      <td>-0.751965</td>\n",
       "      <td>-0.751965</td>\n",
       "      <td>-0.751965</td>\n",
       "      <td>-0.751965</td>\n",
       "      <td>-0.751965</td>\n",
       "      <td>0.751965</td>\n",
       "      <td>0.751965</td>\n",
       "      <td>0.751965</td>\n",
       "      <td>0.751965</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>w_4</th>\n",
       "      <td>0.125000</td>\n",
       "      <td>0.125000</td>\n",
       "      <td>0.125000</td>\n",
       "      <td>0.101850</td>\n",
       "      <td>0.101850</td>\n",
       "      <td>0.101850</td>\n",
       "      <td>0.064820</td>\n",
       "      <td>0.064820</td>\n",
       "      <td>0.064820</td>\n",
       "      <td>0.125000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SUM</th>\n",
       "      <td>0.321313</td>\n",
       "      <td>0.321313</td>\n",
       "      <td>0.321313</td>\n",
       "      <td>-0.525985</td>\n",
       "      <td>-0.525985</td>\n",
       "      <td>-0.525985</td>\n",
       "      <td>0.977945</td>\n",
       "      <td>0.977945</td>\n",
       "      <td>0.977945</td>\n",
       "      <td>-0.321313</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>G(x)</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>-1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              0         1         2         3         4         5         6  \\\n",
       "x      0.000000  1.000000  2.000000  3.000000  4.000000  5.000000  6.000000   \n",
       "y      1.000000  1.000000  1.000000 -1.000000 -1.000000 -1.000000  1.000000   \n",
       "w_1    0.100000  0.100000  0.100000  0.100000  0.100000  0.100000  0.100000   \n",
       "pre_1  1.000000  1.000000  1.000000 -1.000000 -1.000000 -1.000000 -1.000000   \n",
       "g_1    0.423649  0.423649  0.423649 -0.423649 -0.423649 -0.423649 -0.423649   \n",
       "w_2    0.071430  0.071430  0.071430  0.071430  0.071430  0.071430  0.166670   \n",
       "pre_2  1.000000  1.000000  1.000000  1.000000  1.000000  1.000000  1.000000   \n",
       "g_2    0.649629  0.649629  0.649629  0.649629  0.649629  0.649629  0.649629   \n",
       "w_3    0.045460  0.045460  0.045460  0.166660  0.166660  0.166660  0.106060   \n",
       "pre_3 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000  1.000000   \n",
       "g_3   -0.751965 -0.751965 -0.751965 -0.751965 -0.751965 -0.751965  0.751965   \n",
       "w_4    0.125000  0.125000  0.125000  0.101850  0.101850  0.101850  0.064820   \n",
       "SUM    0.321313  0.321313  0.321313 -0.525985 -0.525985 -0.525985  0.977945   \n",
       "G(x)   1.000000  1.000000  1.000000 -1.000000 -1.000000 -1.000000  1.000000   \n",
       "\n",
       "              7         8         9  \n",
       "x      7.000000  8.000000  9.000000  \n",
       "y      1.000000  1.000000 -1.000000  \n",
       "w_1    0.100000  0.100000  0.100000  \n",
       "pre_1 -1.000000 -1.000000 -1.000000  \n",
       "g_1   -0.423649 -0.423649 -0.423649  \n",
       "w_2    0.166670  0.166670  0.071430  \n",
       "pre_2  1.000000  1.000000 -1.000000  \n",
       "g_2    0.649629  0.649629 -0.649629  \n",
       "w_3    0.106060  0.106060  0.045460  \n",
       "pre_3  1.000000  1.000000  1.000000  \n",
       "g_3    0.751965  0.751965  0.751965  \n",
       "w_4    0.064820  0.064820  0.125000  \n",
       "SUM    0.977945  0.977945 -0.321313  \n",
       "G(x)   1.000000  1.000000 -1.000000  "
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.T"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "nbTranslate": {
   "displayLangs": [
    "*"
   ],
   "hotkey": "alt-t",
   "langInMainMenu": true,
   "sourceLang": "en",
   "targetLang": "fr",
   "useGoogleTranslate": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
